import characterTools from "server/component/model/characterTools";
import common from "shared/component/common/common";
import InstanceMap from "shared/component/event/InstanceRemoveEvent";
import Log4R from "shared/component/log/Log4R";
import { AIServiceImpl } from "shared/modules/character/service/AIServiceImpl";
const loginfo = Log4R.getLog('info');
const test: Ctn.AI.Role = {
    thinkTime: 0.3
}

export default class AIServerServiceImpl extends AIServiceImpl {
    /** 设置NPC的网络所有权 */
    setNetWork(npcModel: Model) {
        const players = game.GetService('Players').GetPlayers();
        if (players.size() > 0) {
            npcModel.PrimaryPart?.SetNetworkOwner(players[0]);
        }
    }
    /** 刷新AI */
    initAI(currentScenceEntity: Ctn.AI.AreaConfig) {
        const npcConfigs = new Array<Ctn.AI.AICharacterEntity>()
        currentScenceEntity?.scenceInstance.GetDescendants().forEach(c => {
            if (c.IsA('Configuration') && c.Name === 'mon') {
                /** 怪物生成地点 */
                const monBornPart = c.Parent;
                if (monBornPart?.IsA('Part')) {
                    monBornPart.CanCollide = false;
                    monBornPart.Transparency = 1;
                    monBornPart.GetChildren().forEach(c => {
                        if (c.IsA('Texture')) {
                            c.Transparency = 1;
                        }
                    })
                }
                if (monBornPart?.IsA('Part')) {
                    const ancestors = common.getAncestors(monBornPart);
                    const area = ancestors[2];
                    /** 怪物配置 */
                    const monsterConfig = currentScenceEntity.monster?.[area.Name]?.monster?.[monBornPart.Name];
                    if (monsterConfig) {
                        /** 怪物名称 */
                        const monsterModelName = monsterConfig.character;
                        if (monsterModelName) {
                            /** 怪物模型 */
                            const npcModel = characterTools.createCharacter("M1") as Ctn.character.HumanModel;
                            if (npcModel) {
                                npcModel.Parent = game.Workspace;
                                /** 怪物出生地点 */
                                const bornCframe = characterTools.posCharacter(npcModel.Humanoid, monBornPart);
                                if (bornCframe) {
                                    // 初始化怪物物理                                
                                    const players = game.GetService('Players').GetPlayers();
                                    if (players.size() > 0) {

                                        // alignPosition.Position = bornCframe?.Position;
                                        npcModel.PrimaryPart?.SetNetworkOwner(players[0]);
                                        test.id = "test"
                                        const npcConfig: Ctn.AI.AICharacterEntity = {
                                            character: npcModel,
                                            areaName: area.Name,
                                            bornName: monBornPart.Name,
                                            role: test,
                                            bornCframe,
                                            animGroup: "C1",
                                            skinPath: "M1",
                                            maxJumpTimes: 0,
                                            jumpTimes: 0
                                        };
                                        loginfo?.info('npcConfig', npcConfig)
                                        this.AIConfigMap.set(npcModel, npcConfig)
                                        npcConfigs.push(npcConfig)

                                    }
                                }
                            }
                        }
                    }

                }
            }
        })
        return npcConfigs
    }
}